package com.eims.sls_app.fragment;

import com.eims.sls_app.ui.AboutAppActivity;
import com.eims.sls_app.ui.FollowHelpActivity;
import com.eims.sls_app.ui.MainActivity;
import com.eims.sls_app.ui.SuggestActivity;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.view.MyToast;

import bls.juejin.cn.R;
import android.R.bool;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class myLiveScoresFragment extends Fragment {
	public final static String TAG = "LiveScoresFragment";
	private WebView ls_webview;
private int position;
	private Bundle saveState;
	private String mUrl;
	
	public static Fragment newInstance(int position){
		Fragment liveScoresFragment = new myLiveScoresFragment();
		Bundle bundle = new Bundle();
		//Log.i("Hall", "Betfair"+position);
		bundle.putInt("position", position);
		//position = position;
		liveScoresFragment.setArguments(bundle);
		return liveScoresFragment;
	}
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		//MyToast.getToast(getActivity(), TAG+bundle);
		if (bundle==null) {
			position = 0;
		}else{
			position = bundle.getInt("position");
		}
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Context contextThemeWrapper = new ContextThemeWrapper(getActivity(),
				R.style.StyledIndicators);
		LayoutInflater localInflater = inflater
				.cloneInContext(contextThemeWrapper);
		View v = localInflater.inflate(R.layout.livescores_fragment, container,
				false);
		ls_webview = (WebView) v.findViewById(R.id.live_scores_webview);
		
		ImageView iv_cover = (ImageView)v.findViewById(R.id.iv_cover);
		RelativeLayout rv_main_top = (RelativeLayout)v.findViewById(R.id.rv_main_top);
		rv_main_top.setVisibility(View.GONE);
		
		getsw(position);
		//iv_cover.setVisibility(View.VISIBLE);
		//getWebview();
		//getWebview(App.H5_Live);
		return v;
	}
	

	private void getsw(int position2) {
		switch (position2) {
		case 0:
			getWebview(App.H5_Live);
			break;
		case 1:
			getWebview(App.H5_betfair);
			break;
		case 2:
			getWebview(App.H5_liao);
			break;
		case 3:
			getWebview(App.H5_odds);
			break;

		default:
			break;
		}
		
	}

	private void getWebview(String url) {
		// ls_webview.loadDataWithBaseURL(null, App.H5_Live, "text/html",
		// "utf-8", null);
		WebSettings webSettings = ls_webview.getSettings();

		webSettings.setJavaScriptEnabled(true);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webSettings.setSupportMultipleWindows(true);
		// webSettings.setDisplayZoomControls(false);
		webSettings.setBuiltInZoomControls(false);
		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		webSettings.setDomStorageEnabled(true);

		ls_webview.setWebChromeClient(new WebChromeClient());

		ls_webview.setHorizontalScrollBarEnabled(false);
		ls_webview.setVerticalScrollBarEnabled(false);
		ls_webview.loadUrl(url);
		// ls_webview.loadDataWithBaseURL(null, App.H5_Live, "text/html",
		// "utf-8", null);
		ls_webview.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Log.i(TAG, "-------" + url);
				/*if (url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Flive%2F")
						|| url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fodds%2F")
						|| url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fwlive%2F%232017%2F3%2F22")
						|| url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Flive%2Fmatchs.html")) {
					Intent intent = new Intent(getActivity(),
							AboutAppActivity.class);
					startActivity(intent);
					return true;
				}
				if (url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fdata%2F")
						|| url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fnews%2F")
						|| url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Freport%2F")
						|| url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fblive%2F")) {
					Intent intent = new Intent(getActivity(),
							AboutAppActivity.class);
					startActivity(intent);
					return true;
				}
				if (url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fbodds%2F")
						|| url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fblive%2Fmatchs.html")
						|| url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fbdata%2Fdatum%2Findex.html")
						|| url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fbnews%2Findex.html%3Ftab%3D4")) {
					Intent intent = new Intent(getActivity(),
							AboutAppActivity.class);
					startActivity(intent);
					return true;
				}
				if (url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fbnews%2Findex.html%3Ftab%3D0")
						|| url.endsWith("html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fwlive%2F")) {
					Intent intent = new Intent(getActivity(),
							AboutAppActivity.class);
					startActivity(intent);
					return true;
				}
				if (url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Flive%2F")
						|| url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fodds%2F")
						|| url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fwlive%2F")
						|| url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Flive%2Fmatchs.html")) {
					Intent intent = new Intent(getActivity(),
							SuggestActivity.class);
					startActivity(intent);
					return true;
				}
				if (url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fdata%2F")
						|| url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fnews%2F")
						|| url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Freport%2F")
						|| url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fblive%2F")
						|| url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fbodds%2F")) {
					Intent intent = new Intent(getActivity(),
							SuggestActivity.class);
					startActivity(intent);
					return true;
				}
				if (url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fblive%2Fmatchs.html")
						|| url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fbnews%2Findex.html%3Ftab%3D4")
						|| url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fbdata%2Fdatum%2Findex.html")
						|| url.endsWith("php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Fbnews%2Findex.html%3Ftab%3D0")) {
					Intent intent = new Intent(getActivity(),
							SuggestActivity.class);
					startActivity(intent);
					return true;
				}
				if (url.endsWith("?jumpNone=webapp")) {
					Uri uri = Uri.parse("http://www.juejincai.com/");
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_VIEW);
					intent.setData(uri);
					startActivity(intent);
					return true;
				}

				if (url.endsWith("?Classid=&view=simplify&match=&line=no&jumpNone=webapp")) {
					Uri uri = Uri.parse("http://www.juejincai.com/");
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_VIEW);
					intent.setData(uri);
					startActivity(intent);
					return true;
				}
				if (url.endsWith("s_chkagent_resume.shtml")
						|| url.endsWith("id487115059")
						|| url.endsWith("7MSoccer.apk")) {
					Uri uri = Uri
							.parse("http://www.juejincai.com/Mobile.aspx?from=9");
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_VIEW);
					intent.setData(uri);
					startActivity(intent);
					return true;

				}*/
				// http://3g.7m.cn/feedback_wa.php?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Flive%2F(反馈意见)
				// http://m.7m.com.cn/about.html?l=gb&url=http%3A%2F%2Fm.7m.com.cn%2Flive%2F(关于我们)
				// http://bf.7m.com.cn/default_gb.aspx?Classid=&view=simplify&match=&line=no&jumpNone=webapp(pc端)

				view.loadUrl(url);
				return super.shouldOverrideUrlLoading(view, url);
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {

				super.onReceivedError(view, errorCode, description, failingUrl);
			}
		});
	}



	private void getFocus() {
		getView().setFocusableInTouchMode(true);
		getView().requestFocus();
		getView().setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if ((keyCode == event.KEYCODE_BACK) && ls_webview.canGoBack()) {
					ls_webview.goBack();
					return true;
				}
				return false;
			}
		});
	}

	/*
	 * private void restoreState(){ if (saveState!=null) {
	 * 
	 * } }
	 * 
	 * private Bundle stveState(){ Bundle state = new Bundle();
	 * 
	 * return state; }
	 */

}
